/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.model.newmodel;

import com.cxhd.lis.util.DateUtil;
import com.cxhd.lis.util.ToolDateTime;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 *
 * @author cxhd
 */
@SuppressWarnings("serial")
public class LisMatchinfo extends Model<LisMatchinfo> {

    public static final LisMatchinfo lisSqdDao = new LisMatchinfo();

    public void updateResult(Map<String, String> map, String name) throws Exception {
        String date = map.get("date");
        String pk = map.get("pk");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        java.util.Date udate = sdf.parse(date);
        java.sql.Timestamp time = new java.sql.Timestamp(udate.getTime());
        findById(pk)
                .set("RESULTSTATUS", "2")
                .set("CHECKOPERATOR", name)
                .set("CHECKTIME", time)
                .update();
    }

    public void saveMatch(Controller con, String req) throws Exception {

        Map<String, String> patInfo = new Gson().fromJson(con.getPara("patInfo"), new TypeToken<Map<String, String>>() {
        }.getType());
        String infopk = patInfo.get("INFOPK");
        if (infopk == null) {//判断是否是条码病人
            infopk = new LisPatient().saveInfo(patInfo);
        }

//        Date thisdate=new Date(System.currentTimeMillis());
//        java.sql.Timestamp thisTime=new java.sql.Timestamp(thisdate.getTime());
        set("PK", getPk())
                .set("STAYHOSPITALMODE", patInfo.get("STAYHOSPITALMODE"))
                .set("UUID", con.getPara("UUID"))
                .set("INFOPK", patInfo.get("INFOPK"))
                .set("SAMPLENO", patInfo.get("SAMPLENO"))
                .set("BARCODE", patInfo.get("BARCODE"))
                .set("RECEIVER", req)
                .set("RECEIVETIME", DateUtil.getDateEN())
                .set("RESULTSTATUS", "1")
                .set("SECTION", patInfo.get("SECTION"))
                .set("BED_NO", patInfo.get("BED_NO"))
                .set("EXAMINAIM", patInfo.get("EXAMINAIM"))
                .set("EXAMINAIMCODE", patInfo.get("EXAMINAIMCODE"))
                .save();
    }

    public void updateXX(Controller con) throws Exception {
        findById(con.getPara("brpk"))
                .set("SJYS", con.getPara("sjys"))
                .set("JYYS", con.getPara("jyys"))
                .update();
    }

    public void updateDY(Controller con) throws Exception {
        String time = con.getPara("time");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        java.util.Date date = sdf.parse(time);
        java.sql.Timestamp stp = new java.sql.Timestamp(date.getTime());
        findById(con.getPara("pk"))
                .set("PRINTTIME", stp)
                .set("RESULTSTATUS", "3")
                .update();
    }

    public void deleteInfo(Controller con) throws Exception {
        Db.use("mysql").update("DELETE from lis_matchinfo WHERE UUID='" + con.getPara("UUID") + "'");
    }

    public String getPk() {
        List<Record> list = Db.use("mysql").find("select * from lis_matchinfo limit 0,10");
        String value = "1";
        if (list.size() > 0) {
            value = (Integer.parseInt(Db.use("mysql").findFirst("SELECT MAX(PK) as id from lis_matchinfo").get("id") + "") + 1) + "";
        }
        return value;
    }
}
